/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zyyoona7.picker.util;

import com.zyyoona7.picker.ResourceTable;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

/**
 * 描述
 *
 * @author name
 * @since 2021-04-14
 */
public class SelectButton extends Button implements Component.TouchEventListener {
    private Context context;

    /**
     * 构造
     *
     * @param context 上下文
     */
    public SelectButton(Context context) {
        super(context);
        this.context = context;
        this.setTouchEventListener(this);
    }

    /**
     * 构造
     *
     * @param context 上下文
     * @param attrSet 属性
     */
    public SelectButton(Context context, AttrSet attrSet) {
        super(context, attrSet);
        this.context = context;
        this.setTouchEventListener(this);
        ShapeElement shapeElement = new ShapeElement(context, ResourceTable.Graphic_background_button);
        shapeElement.setShape(ShapeElement.RECTANGLE);
        this.setBackground(shapeElement);
    }

    /**
     * 构造
     *
     * @param context 上下文
     * @param attrSet 属性
     * @param styleName style
     */
    public SelectButton(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        this.context = context;
        this.setTouchEventListener(this);
    }

    /**
     * onTouchEvent
     *
     * @param component
     * @param touchEvent
     * @return boolean
     */
    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                ShapeElement shapeElement = new ShapeElement(context,
                        ResourceTable.Graphic_background_button_h);
                shapeElement.setShape(ShapeElement.RECTANGLE);
                this.setBackground(shapeElement);
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                ShapeElement shapeElement2 = new ShapeElement(context, ResourceTable.Graphic_background_button);
                shapeElement2.setShape(ShapeElement.RECTANGLE);
                this.setBackground(shapeElement2);
                break;
            case TouchEvent.CANCEL:
                ShapeElement shapeElement3 = new ShapeElement(context, ResourceTable.Graphic_background_button);
                shapeElement3.setShape(ShapeElement.RECTANGLE);
                this.setBackground(shapeElement3);
                break;
            default:
                break;
        }
        return true;
    }
}
